//
//  WKSharePopView.m
//  Walk
//
//  Created by cesh on 16/9/29.
//  Copyright © 2016年 cesh. All rights reserved.
//

#import "WKSharePopView.h"
#import "WXApi.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <UMSocialCore/UMSocialCore.h>
#import <Masonry.h>

@interface WKSharePopView ()

@property (strong, nonatomic) UIToolbar *glassBG;

@property (weak, nonatomic) IBOutlet UIButton *weChatFriend;
@property (weak, nonatomic) IBOutlet UILabel *weChatFriendLbl;

@property (weak, nonatomic) IBOutlet UIButton *weChatMoment;
@property (weak, nonatomic) IBOutlet UILabel *weChatMomentLbl;

@property (weak, nonatomic) IBOutlet UIButton *QQFriend;
@property (weak, nonatomic) IBOutlet UILabel *QQFriendLbl;

@property (weak, nonatomic) IBOutlet UIButton *QQZone;
@property (weak, nonatomic) IBOutlet UILabel *QQZoneLbl;

@end

@implementation WKSharePopView

- (void)awakeFromNib {
    [super awakeFromNib];
    if (![WXApi isWXAppInstalled]) {
        self.weChatFriend.hidden = YES;
        self.weChatMoment.hidden = YES;
        self.weChatMomentLbl.hidden = YES;
        self.weChatFriendLbl.hidden = YES;
    }
    if (![QQApiInterface isQQInstalled]) {
        self.QQZone.hidden = YES;
        self.QQFriend.hidden = YES;
        self.QQZoneLbl.hidden = YES;
        self.QQFriendLbl.hidden = YES;
    }
}

- (IBAction)shareBtn:(UIButton *)button {
    UMSocialPlatformType socialPlatformType;
    switch (button.tag) {
        case 1:
            socialPlatformType = UMSocialPlatformType_WechatSession;
            break;
        case 2:
            socialPlatformType = UMSocialPlatformType_WechatTimeLine;
            break;
        case 3:
            socialPlatformType = UMSocialPlatformType_QQ;
            break;
        case 4:
            socialPlatformType = UMSocialPlatformType_Qzone;
            break;
        default:
            break;
    }
    
    [self shareDataWithPlatform:socialPlatformType];
}



- (void)shareDataWithPlatform:(UMSocialPlatformType)platformType
{
    // 创建UMSocialMessageObject实例进行分享
    // 分享数据对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    
    
    
    NSString *title = @"蛙客Walk";
    NSString *text = @"快点来参加我发起的活动吧，就差你一了个哟~!";
    UIImage *image = [UIImage imageNamed:@"AppIcon"];
    
    UMShareWebpageObject *webObject = [UMShareWebpageObject shareObjectWithTitle:title descr:text thumImage:image];
    NSString *eventID = [[NSUserDefaults standardUserDefaults]objectForKey:@"eventID"];
    [webObject setWebpageUrl:[NSString stringWithFormat:@"http://junvalley.com/walk/Web/share?eid=%@",eventID]];
    
    messageObject.shareObject = webObject;
    
    
    [[UMSocialManager defaultManager]shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id result, NSError *error) {
        NSString *message = nil;
        if (!error) {
            [self dismiss:nil];
            message = [NSString stringWithFormat:@"分享成功"];
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];
        } else {
            [self dismiss:nil];
            message = [NSString stringWithFormat:@"失败原因Code: %d\n",(int)error.code];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }
     ];
}
- (IBAction)dismiss:(id)sender {
    [WKPopView dismiss];
    [self removeFromSuperview];
}

@end
